<template>
   <div>
    <div class="goods">
      <div class="menu-wrapper">
        <ul  class="ulBox" ref='ulBox'>
          <!--current 表示高亮-->
         
           <!-- :class="{current: index===currentIndex}" -->
          <li class="menu-item" v-for="(good, index) in goods" :key="index"
               :class="{current:index===currentIndex}"
               @click="clickMenuItem(index)"

              >
            <span class="text bottom-border-1px">
               <!-- :src="good.icon" v-if="good.icon" -->
              <img class="icon"  :src='good.icon'   v-if="good.icon"> 
              {{good.name}}
            </span>
          </li>
        </ul>
      </div>
      <div class="foods-wrapper">
        <ul ref="foodsUl" class="foodsUl">
          <li class="food-list-hook" v-for="(good, index) in goods" :key="index">
            <h1 class="title">{{good.name}}</h1>
            <ul>
               <!-- @click="showFood(food)" -->
                  <!-- @click="showFood(food)" -->
              <li class="food-item bottom-border-1px" v-for="(food, index) in good.foods"
                  :key="index" 
                  

                 
                  
                 
                  >
                <div class="icon">
                  <img width="57" height="57" :src="food.icon">
                </div>
                <div class="content">
                  <h2 class="name">{{food.name}}</h2>
                  <p class="desc">{{food.description}}</p>
                  <div class="extra">
                    <span class="count">月售{{food.sellCount}}份</span>
                    <span>好评率{{food.rating}}%</span>
                  </div>
                  <div class="price">
                    <span class="now">￥{{food.price}}</span>
                    <span class="old" v-if="food.oldPrice">￥{{food.oldPrice}}</span>
                  </div>
                  <div class="cartcontrol-wrapper">
                   <!-- 购物车 的商品计算 -->
                        <!-- <CartControl :food='food'/> -->
                  </div>
                </div>
              </li>
            </ul>
          </li>
        </ul>
      </div>
      <!-- 购物车结算组件 -->
      <!-- <ShopCart /> -->
    </div>
    <!-- <Food :food="showMyfood" ref="food"/> -->
    <!-- 1 父组件向子组件传递值 props 接受 -->
    <!--  2 子组件向父组件传递值（传递的一个方法）$emit -->
    <!-- 3  父组件调用子组件的方法 -->
  </div>
</template>

<script>
/*需求
 1  两边应该都需要滑动  
 2 左边要实现 点击高亮 同时滑动右边的对应的商品展示
 3 右边滑动 同时也需要对应切换左边导航栏对应的商品高亮  
*/

// 1 引入better-scroll
import BetterScroll from 'better-scroll'

// 实例化
import  {mapActions,mapState} from 'vuex'
export default {
  name: '',
  data () {
    return {
       tops:[], //存放每个一个右边li的滚出去的高度
      scrollY:'',//滚动的时候需要滚出去的距离  
    }
  },
 
  computed: {
   ...mapState(['goods']),
    currentIndex() {
      let {tops,scrollY}=this;
     var Index=  tops.findIndex((item,index)=>{
       return    scrollY>=tops[index]&&scrollY<tops[index+1]
      })


      console.log(Index)
      return Index;


    }
  },
  mounted(){
     this.getShopGoods(()=>{
       this.$nextTick(()=>{
         this._getScroll();
           this._initFoods()
       })
     });
  },
   methods: {
    ...mapActions(['getShopGoods']),
     _getScroll() {

       this.menu=  new  BetterScroll('.menu-wrapper',{
              probeType:3,
            click:true
          });



          this.foods=new   BetterScroll('.foods-wrapper',{
            probeType:2,
            click:true
          });


          this.foods.on('scroll',({x,y})=>{
              
            // console.log(x)
                  // console.log(y)

          })

        this.foods.on('scrollEnd',({x,y})=>{
              console.log(y)
            let myscrollY=Math.abs(y);
            this.scrollY=myscrollY
          
          })

          },


        _initFoods(){
                let tops=[];
                  let top=0;
                  tops.push(top);

            // var  lis=document.querySelectorAll('.foodsUl>li');
            // console.log(lis);
            var  lis= this.$refs.foodsUl.children;
              // console.log(lis); //这是一个伪数组 
              Array.from(lis,(eleLi,index)=>{
                // console.log(eleLi)
              var  h=  eleLi.clientHeight;

              // console.log(h)
              top+=h;
               tops.push(top);


              })

             console.log(tops);

             this.tops=tops

          
        } ,
        
        //点击左边的按钮实现当前高亮   https://www.jianshu.com/p/0f79a65b40e1
        clickMenuItem(myIndex) {
          console.log(myIndex);

          // scrollY  唯一条件就是动这个值 这个值改变了 那么 currentIndex 会发生变化
          // currentIndex 发生变化了  current class就会发生变化\

      //   scrollTo(x, y, time, easing); => 滚动到指定的位置
      // x: x轴位置，
      // y: y轴位置
      // time: 滚动动画时长
      // easing: 缓动函数 不建议修改
       
       let topHeight=this.tops[myIndex];

      this.foods.scrollTo(0,-topHeight,300)

        }
        
       

  }
}




</script>

<style scoped  lang='stylus' rel='stylesheet/stylus'>
 @import "../../../common/mixins.styl"
  .goods
    display: flex
    position: absolute
    top: 215px
    bottom: 46px
    width: 100%
    background: #fff;
    overflow: hidden
    .menu-wrapper
      flex: 0 0 80px
      width: 80px
      background: #f3f5f7
      .menu-item
        display: table
        height: 54px
        width: 56px
        padding: 0 12px
        line-height: 14px
        &.current
          position: relative
          z-index: 10
          margin-top: -1px
          background: #fff
          color: $green
          font-weight: 700
          .text
            border-none()
        .icon
          display: inline-block
          vertical-align: top
          width: 12px
          height: 12px
          margin-right: 2px
          background-size: 12px 12px
          background-repeat: no-repeat
        .text
          display: table-cell
          width: 56px
          vertical-align: middle
          bottom-border-1px(rgba(7, 17, 27, 0.1))
          font-size: 12px
    .foods-wrapper
      flex: 1
      .title
        padding-left: 14px
        height: 26px
        line-height: 26px
        border-left: 2px solid #d9dde1
        font-size: 12px
        color: rgb(147, 153, 159)
        background: #f3f5f7
      .food-item
        display: flex
        margin: 18px
        padding-bottom: 18px
        bottom-border-1px(rgba(7, 17, 27, 0.1))
        &:last-child
          border-none()
          margin-bottom: 0
        .icon
          flex: 0 0 57px
          margin-right: 10px
        .content
          flex: 1
          .name
            margin: 2px 0 8px 0
            height: 14px
            line-height: 14px
            font-size: 14px
            color: rgb(7, 17, 27)
          .desc, .extra
            line-height: 10px
            font-size: 10px
            color: rgb(147, 153, 159)
          .desc
            line-height: 12px
            margin-bottom: 8px
          .extra
            .count
              margin-right: 12px
          .price
            font-weight: 700
            line-height: 24px
            .now
              margin-right: 8px
              font-size: 14px
              color: rgb(240, 20, 20)
            .old
              text-decoration: line-through
              font-size: 10px
              color: rgb(147, 153, 159)
          .cartcontrol-wrapper
            position: absolute
            right: 0
            bottom: 12px
</style>